package com.itheima.controller;




import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.common.ResultData;
import com.itheima.domain.UmsResource;
import com.itheima.domain.UmsRole;
import com.itheima.manager.UmsRoleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@RestController
@RequestMapping("/role")
@CrossOrigin(origins = "*")
public class UmsRoleController {

    @Autowired
    private UmsRoleManager umsRoleManager;

    // 查询全部
    @GetMapping("/listAll")
    public ResultData listAll(){
        return umsRoleManager.getAll();
    }

    // 分页查询
    @GetMapping("/list")
    public ResultData listByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     String keyword){
       return umsRoleManager.getListByPage(pageNum, pageSize, keyword);

    }

    // 添加角色
    @PostMapping("/create")
    public ResultData create(@RequestBody UmsRole umsRole){
       return umsRoleManager.create(umsRole);
    }

    // 批量删除角色
    @PostMapping("/delete")
    public ResultData delete(@RequestParam List<Long> ids){
        return umsRoleManager.delete(ids);
    }

    // 修改角色
    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable Integer id,@RequestBody UmsRole umsRole){
       return umsRoleManager.update(umsRole);
    }

    // 修改角色状态
    @PostMapping("/updateStatus/{id}")
    public ResultData updateStatus(@PathVariable("id") Integer id, Integer status){
        return umsRoleManager.updateStatus(id, status);
    }

    // 获取角色相关菜单
    @GetMapping("/listMenu/{roleId}")
    public ResultData listMenu(@PathVariable Long roleId){
       return umsRoleManager.listMenu(roleId);
    }

    // 给角色分配菜单
    @PostMapping("/allocMenu")
    public ResultData allocMenu(@RequestParam("menuIds") List<Long> menuIds,
                                @RequestParam("roleId") Long roleId){
        return umsRoleManager.allocMenu(roleId, menuIds);
    }

    // 获取角色相关资源
    @GetMapping("/listResource/{roleId}")
    public ResultData listResource(@PathVariable("roleId") Long roleId){
        return umsRoleManager.listResource(roleId);
    }

    // 给角色分配资源
    @PostMapping("/allocResource")
    public ResultData allocResource(@RequestParam("resourceIds") List<Long> resourceIds,
                                    @RequestParam("roleId") Long roleId){
        return umsRoleManager.allocResource(roleId, resourceIds);
    }
}

